package seo.dale.util.datastructure;

public class ArrayBasedCapacityFixedStack<E> implements Stack<E> {
	
	private Object[] array;
	
	/**
	 * The size of this stack.
	 */
	private int size;
	
	public ArrayBasedCapacityFixedStack(int capacity) {
		array = new Object[capacity];
		size = 0;
	}

	@Override
	public E push(E item) {
		if (isFull()) {
			throw new RuntimeException("Stack is full.");
		}
		array[size++] = item;
		return item;
	}

	@Override
	@SuppressWarnings("unchecked")
	public E peek() {
		if (isEmpty()) {
			throw new RuntimeException("Stack is empty.");
		}
		return (E) array[size - 1];
	}

	@Override
	public E pop() {
		E item = peek();
		array[--size] = null;
		return item;
	}

	@Override
	public boolean isEmpty() {
		return size == 0;
	}
	
	private boolean isFull() {
		return size == array.length;
	}

}
